var app = getApp();
var https = "https://www.moreclub.cn/";
var http1 = 'http://10.150.7.248:8001/';
var http2 = 'http://59.110.53.149:8088/'
var istrue = false;
var isInto = true;
var cityId;
var pn;
var ps;
var idn;
var showdiv = false


var add0 = function (m) {
  return m < 10 ? '0' + m : m
}
var time = function (prefeerTime) {
  var time = new Date(prefeerTime);
  // var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
   var h = time.getHours();
   var mm = time.getMinutes();
  // var s = time.getSeconds();
  // return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
  return add0(m) + '月' + add0(d)+'日'+' '+ add0(h) +':'+add0(mm)
}

var getList=function(that){
    var uid = wx.getStorageSync('uid');
    var access_token = wx.getStorageSync('inToken');
    wx.request({
      url: https + 'v3/prefeer/open/listbyCity?city='+cityId+'&uid=' + uid ,
      data: {
        page: pn,
        pageSize: 5
      },
      success: function (res) {
        console.log(res)
        if (res.data.success) {
          var pubData = res.data.data
          try {
            wx.setStorageSync('pubData', pubData);
          } catch (e) {
          }

          var pubList = [];//创建一个新数组
          for (var key in pubData) {
            var fromThumb = pubData[key].fromThumb;//头像
            if (pubData[key].prefeerPictrues){
              var photos = pubData[key].prefeerPictrues;//图片
              var photo = photos.split(",");
            }
           
            if (pubData[key].prefeerContent){
              var prefeerContent = pubData[key].prefeerContent;//内容
              var contentArr = prefeerContent.split('\n');
            }
            
            var nickName = pubData[key].nickName;
            if (pubData[key].likeDto.userLikes){
               var userLikes = pubData[key].likeDto.userLikes.reverse();//点赞人显示
            }else{
              var userLikes = pubData[key].likeDto.userLikes
            } 
            
            var prefeerTime = pubData[key].prefeerTime;//时间
            var pid = pubData[key].pid;
            var chainMerchants = pubData[key].chainMerchants;
            var chainInter = pubData[key].chainInter;
            var isClick = pubData[key].likeDto.clicked;
            var likeTimes = pubData[key].likeDto.likeTimes
            var subType = pubData[key].subType;
            var sourceFrom = pubData[key].sourceFrom;//more出品
            var uid1 = pubData[key].uid;
            var type1 = pubData[key].type;
            var commentCount = pubData[key].commentCount;//评论人数
            var comments = pubData[key].comments;//评论内容
            time(prefeerTime)
            var dataTime = time(prefeerTime)
            
            // for (var i = 0; i < pubData[key].likeDto.userLikes.length;i++){
            //   if (uid == pubData[key].likeDto.userLikes[i].uid) {
            //       reday = true
            //      break;
            //   }else{
            //      reday = false
            //   }
            // }
           

            var obj = {};//创建新对象
            obj.fromThumb = fromThumb;
            obj.nickName = nickName;
            obj.prefeerContent = prefeerContent;
            obj.dataTime = dataTime;
            obj.chainInter = chainInter;
            obj.photos = photos;
            obj.chainMerchants = chainMerchants;
            obj.photo = photo;
            obj.isClick = isClick;
            obj.likeTimes = likeTimes;
            obj.subType = subType;
            obj.sourceFrom = sourceFrom;
            obj.type1 = type1;
            obj.pid = pid;
            obj.uid1 = uid1;
            obj.contentArr = contentArr;
            obj.userLikes = userLikes;
            obj.commentCount = commentCount;
            obj.comments = comments;
            pubList.push(obj);
          };
          console.log(pubList)
          if (pubList != null) {
            
            var oldlist = that.data.totallist;
            for (var i = 0; i < pubList.length; i++) {
              oldlist.push(pubList[i])
            }
          }
          that.setData({
            pubList: pubList,
            totallist:oldlist,
            uid:uid
          })

        }
      }
    })
}

Page({
  data:{
    totallist:[],
    showBack:false
  },
  onLoad:function(options){
     var that = this; 
     var uid = wx.getStorageSync('uid');
     console.log(options)
    if (options.midname){
      that.setData({
        midname: options.midname,
        uid:uid
      })
    }
  },
  onReady:function(){
    var that = this;
    cityId = wx.getStorageSync('cityId')
    if (cityId == '' || !cityId || typeof (cityId) == "undefined"){
      cityId = '2'
    }
    if(!isInto){
      var total = wx.getStorageSync('total');
      that.setData({
        totallist:total
      })
    }else{
      pn=0
      getList(that)
    }
     
  },

  showdiv:function(e){
    var that = this;
    idn = e.currentTarget.dataset.idn
    that.setData({
      showdiv:true,
      idn:idn
    })
  },
  hidediv:function(){
    var that = this;
    that.setData({
      showdiv:false
    })
  },

  deleteBtn:function(e){
    //删除动态
    var that = this
    var access_token = wx.getStorageSync('inToken');

    console.log(e)
    var pid = e.currentTarget.dataset.pid
    wx.request({
      url: https + 'v2/ugc/delete/'+pid+'?access_token=' + access_token,
      method: 'DELETE',
      header: {
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res)
        if(res.data.data){
          var totallist = that.data.totallist
          totallist.splice(idn,1)
          that.setData({
            totallist: totallist
          })
        }
      }
    })
    that.setData({
      showdiv: false
    })
  },
  
  
  praiseBtn:function(e){
     console.log(e) 
     var that = this; 
     var uid = wx.getStorageSync('uid');
     var access_token = wx.getStorageSync('inToken');
     var total = that.data.totallist;
     var thumb = wx.getStorageSync('thumb');
     var nickName = wx.getStorageSync('myUsername');
     var idx1 = e.currentTarget.dataset.idx;
     isInto = false
     
    //  wx.request({
    //    url: https + 'v1/information/aordeliketime?uid=' + uid + '&fid=' + total[idx1].pid + '&type=' + total[idx1].type1 + '&access_token=' + access_token,
    //    success: function (res) {
    //      console.log(res)
    //    }
    //  })
    
     if (total[idx1].isClick && total[idx1].userLikes!=''){
  
       for(var j in total[idx1].userLikes){
         if (total[idx1].userLikes[j].uid==uid){
           var n = j;
           total[idx1].userLikes.splice(n,1)
           
           total[idx1].userLikes = total[idx1].userLikes
           total[idx1].isClick = false
           total[idx1].likeTimes -= 1
           that.setData({
               totallist:total,
               idx1: idx1,
               isClick: total[idx1].isClick
           })
         }
       }
       try {
         wx.setStorageSync('total', total);
       } catch (e) {
       }
       
     } else{
          if (total[idx1].userLikes){
            var o = {}
            o.thumb = thumb;
            o.uid = uid;
            o.nickName = nickName;
            total[idx1].userLikes.push(o)
            total[idx1].isClick = true
            total[idx1].likeTimes += 1
            that.setData({
              totallist: total,
              idx1: idx1,
              isClick: total[idx1].isClick
            })
            try {
              wx.setStorageSync('total', total);
            } catch (e) {
            }   
          }else{
            var user = []
            var o = {}
            o.thumb = thumb;
            o.uid = uid;
            o.nickName = nickName;
            user.push(o)
            total[idx1].userLikes = user
            total[idx1].isClick = true
            total[idx1].likeTimes += 1
            console.log(total)
            that.setData({
              totallist: total,
              idx1: idx1,
              isClick: total[idx1].isClick
            })
            try {
              wx.setStorageSync('total', total);
            } catch (e) {
            }   
          }
        
     }
    
    
  },

  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;
    cityId = wx.getStorageSync('cityId')
    if(cityId==''||!cityId){
      cityId==2
    }
    if (that.data.pubList != null && that.data.pubList.length != 0) {
      pn += 1;
      getList(that);
    } else {
      wx.showToast({
        title: '已加载完成',
        duration: 1000
      })
    }
  },

  // onShareAppMessage: function () {
  //   console.log('城市' + city)
  //   return {
  //     title: '全城优质*酒吧推荐', // 分享标题
  //     desc: '品酒，泡吧，看演出，听音乐，认识新朋友，有趣好玩就在More App。', // 分享描述
  //     path: '/pages/alllist/alllist' + "?city=" + city + '&pn=0' + '&lat1=' + lat1 + '&lng1=' + lng1// 分享路径
  //   }
  // }
})  